//==============================================================================
//
//               Copyright (C) 2002  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-02-01 08:43:02 $ $Revision: 1.4 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DText.h"
#include "ofc/DInt.h"
#include "ofc/DRegEx.h"

#include "DInc.h"
#include "DTest.h"

//-Wrappers--------------------------------------------------------------------

void DRegEx_test()
{
#ifdef HAVE_DREGEX
  DRegEx *expr = [DRegEx alloc];
  DArray *subs;
  
  STARTTEST();

  [expr init :"ba*b"];
  
  TEST([expr match :"baaab"] == 5);
  TEST([expr match :"baAab"] == DRE_NO_MATCH);
  TEST([expr match :"bbbab"  :2] == 3);
  TEST([expr match :"baaaab" :6 :1] == DRE_NO_MATCH);

  [expr ccompile :"[[:space:]]*"];
  
  TEST([expr match :" \t a"] == 3);
  
  [expr icompile :"ba*b"];
  
  TEST([expr match :"baaab"] == 5);
  TEST([expr match :"bbaAab" :6 :1] == 5);
  
  [expr ccompile :"[a-zA-Z_][a-zA-Z_0-9]*"];
  
  TEST([expr match :"_a123"] == 5);
  TEST([expr match :"Zaar5"] == 5);
  TEST([expr match :"12345"] == DRE_NO_MATCH);
  
  [expr ccompile :"-?[0-9]+(\.[0-9]+)?"];
  
  TEST([expr match :"-12.7"] == 5);
  TEST([expr match :"+12.7"] == DRE_NO_MATCH);
  TEST([expr match :".34"]   == DRE_NO_MATCH);
  TEST([expr match :"123."]  == 3);
  TEST([expr match :"1234.6."] == 6);

  [expr ccompile :"a(b)*c"];

  TEST([expr match   :"abbbc"] == 5);
  
  subs = [expr indices];
  TEST(subs != nil);
  TEST([subs length] == 4);
  TEST([((DInt *)[subs get :0]) get] == 0);
  TEST([((DInt *)[subs get :1]) get] == 4);
  TEST([((DInt *)[subs get :2]) get] == 3); // repetition: only the last match
  TEST([((DInt *)[subs get :3]) get] == 3);
  
  [subs free];

  TEST([expr match :"cde"] == DRE_NO_MATCH);
  TEST([expr indices] == nil);
  
  [expr free];

  expr = [DRegEx new];
  
  [expr ccompile :"b(a{2,4})"];
  
  TEST([expr search :"vjjjdbaaaaa"] == 5);
  
  subs = [expr matches :"vjjjdbaaaaa"];
  
  TEST(subs != nil);
  TEST([subs length] == 2);
  
  TEST([(DText *)[subs get :0] ccompare :"baaaa"] == 0);
  TEST([(DText *)[subs get :1] ccompare :"aaaa" ] == 0);
  
  [subs free];
  
  TEST([expr search :"abaabaaa" :8] == 1);
  TEST([expr search :"abaabaaa" :2 :6] == 4); 
  TEST([expr search :"abaabaaa" :8 :3 :7] == 4);

  subs = [expr matches :"abaabaaa" :8];
  
  TEST(subs != nil);
  TEST([subs length] == 2);

  TEST([(DData *)[subs get :0] bcompare :"baaa" :4] == 0);
  TEST([(DData *)[subs get :1] bcompare :"aaa"  :3] == 0);

  [subs free];
  [expr free];
  
  expr = [DRegEx new];

  [expr free];
  
  STOPTEST();
#endif
}


